<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Creditor extends CI_Controller {

	private $tab ='borrow';

	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}

	/**
	 * 待审核列表
	 */
	function verify_list(){
		$data['fstatus'] = intval($this->input->get('fstatus'));
		$data['title'] = trim($this->input->get('title'));
		$data['username'] = trim($this->input->get('username'));
		$data['per_page'] = intval($this->input->get('per_page'));
		$data['page_size'] = YTZ_LIST_PAGE_SIZE;
		$data['order'] = 'fstatus';
		$this->load->model('Creditor_model');
		$vdata = $this->Creditor_model->transfer_obj_list($data);
		$vdata['where'] = $data;

		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/creditor/verify_list/?title='.$title.'&username='.$username.'&fstatus='.$fstatus;
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = YTZ_LIST_PAGE_SIZE;
		$this->pagination->initialize($config);

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'creditor_first';

		$this->load->view('/ychadmin/creditor_list', $vdata);
	}

	/**
	 * 待复审列表
	 */
	function reverify_list(){
		$this->load->model('Creditor_model');
		$vdata['list'] = $this->Creditor_model->reverify_list();

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'creditor_second';

		$this->load->view('/ychadmin/creditor_reverify_list', $vdata);
	}

	/**
	 * 审核展示
	 * @param $id
	 */
	function verify($id=0){
		$id = intval($id);
		$this->load->model('Creditor_model');
		$obj = $this->Creditor_model->get_obj_by_id($id);
		$collection_list = $this->Creditor_model->get_collection_by_tenderid($obj['tender_id']);
		$vdata['obj'] = $obj;
		$vdata['coll_list'] = $collection_list;

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'creditor_first';

		$this->load->view('/ychadmin/creditor_view', $vdata);
	}

	/**
	 * 审核操作
	 */
	function doverify(){
		$id = intval($this->input->post('id'));
		$fstatus = intval($this->input->post('fstatus'));
		$this->load->model('Creditor_model');
		$obj = $this->Creditor_model->get_obj_by_id($id);
		$admin = $this->session->userdata('admin_user');
		if ($id>0 && in_array($fstatus, array(2,4)) && $obj['fstatus']==1){
			$data = array(
				'id' => $id,
				'fstatus' => $fstatus,
				'verify_remark' => trim($this->input->post('verify_remark')),
				'tender_id' => $obj['tender_id'],
				'admin' => $admin['user_id']
			);
			if ($data['verify_remark']==''){
				$data['verify_remark'] = $fstatus==2?'审核通过':'审核不通过';
			}
			$ret = $this->Creditor_model->verify($data);
			//更新首页缓存
			$creditor_key = 'ych_creditor_index_list';
			$this->mycache->delete($creditor_key);
			echo $ret ? '审核成功' : '操作失败';
			exit;
		}else{
			echo '数据错误';
			exit;
		}
	}

	/**
	 * 复审展示
	 * @param $id
	 */
	function reverify($id=0){
		$id = intval($id);
		$this->load->model('Creditor_model');
		$obj = $this->Creditor_model->get_obj_by_id($id);
		$buyers = $this->Creditor_model->get_buyers($id);

		$vdata['obj'] = $obj;
		$vdata['buyers'] = $buyers;


		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'creditor_second';


		$this->load->view('/ychadmin/creditor_review', $vdata);
	}

	/**
	 * 复审操作
	 */
	function doreverify(){
		$id = intval($this->input->get_post('id'));
		$fstatus = intval($this->input->get_post('fstatus'));
		$this->load->model('Creditor_model');
		$obj = $this->Creditor_model->get_obj_by_id($id);
		$admin = $this->session->userdata('admin_user');
		if ($id>0 && in_array($fstatus, array(3,4)) && $obj['fstatus']==2){
			//复审通过：购买人扣除冻结资金，添加待收金额，出售人添加可用金额，扣除待收金额
			if ($fstatus==3){
				if ($obj['wait_account']>0){

			  		$tenderlist = $this->Creditor_model->get_buyers($id);
			  		$LoanNoList = "";
			  		foreach ($tenderlist as $tender){
			  			$LoanNoList .= $tender['mmm_loanno'].",";
					}
//					$LoanNoList = substr($LoanNoList,0,strlen($LoanNoList)-1);
//					self::toloantransferaudit($LoanNoList, $id, 3, 1, 1);

					$ret = $this->Creditor_model->reverify_true($id);
					echo $ret ? '复审成功':'操作失败';
					exit;
				}else{
					$ret = $this->Creditor_model->reverify_false($id);
					echo $ret ? '该借款已还清，债权转让自动取消':'操作失败';
					exit;
				}
			}
			//复审不通过：取消冻结资金
			elseif ($fstatus==4){
				$tenderlist = $this->Creditor_model->get_buyers($id);
		  		$LoanNoList = "";
		  		foreach ($tenderlist as $tender){
		  			$LoanNoList .= $tender['mmm_loanno'].",";
				}
				$LoanNoList = substr($LoanNoList,0,strlen($LoanNoList)-1);
				self::toloantransferaudit($LoanNoList, $id, 4, 1, 1);
				/*$ret = $this->Creditor_model->reverify_false($id);
				echo $ret ? '操作成功':'操作失败';
				exit;*/
			}

		}else{
			echo '数据错误';
			exit;
		}

	}



	public function toloantransferaudit($LoanNoList, $id, $borrow_fstatus, $index, $indexall){
		$this->load->library('/mmm/Rsa', 'rsa');
		$this->load->library('/mmm/Common', 'common');

		$PlatformMoneymoremore = MONEYMOREMOREPID;
		if($borrow_fstatus == 3){
			$AuditType = "1";
		}else{
			$AuditType = "2";
		}

		if($this->common->getAntiState() == 0){
			$RandomTimeStamp = "";
		}else if($this->common->getAntiState() == 1){
			$RandomTimeStamp = $this->common->getRandomTimeStamp();
		}
		$Remark1 = $id;
		$Remark2 = $borrow_fstatus;
		$Remark3 = $index.'='.$indexall;
		$ReturnURL = "http://".$_SERVER["HTTP_HOST"]."/moneymoremore/transferauditreturn_creditor.html";
		$NotifyURL = "http://".$_SERVER["HTTP_HOST"]."/moneymoremore/transferauditnotify_creditor.html";
		$dataStr = $LoanNoList.$PlatformMoneymoremore.$AuditType.$RandomTimeStamp.$Remark1.$Remark2.$Remark3.$ReturnURL.$NotifyURL;
		if($this->common->getAntiState() == 1){
			$dataStr = strtoupper(md5($dataStr));
		}
		$SignInfo=$this->rsa->sign($dataStr);

		$officialAction = MONEYMOREMOREURL."/loan/toloantransferaudit.action";

		echo "<form id=\"formu\" name=\"formu\" action=\"".$officialAction."\" method=\"post\">";
		echo "<input id=\"LoanNoList\" name=\"LoanNoList\" value=\"".$LoanNoList."\" type=\"hidden\" />";
		echo "<input id=\"PlatformMoneymoremore\" name=\"PlatformMoneymoremore\" value=\"".$PlatformMoneymoremore."\" type=\"hidden\" />";
		echo "<input id=\"AuditType\" name=\"AuditType\" value=\"".$AuditType."\" type=\"hidden\" />";
		echo "<input id=\"RandomTimeStamp\" name=\"RandomTimeStamp\" value=\"".$RandomTimeStamp."\" type=\"hidden\" />";
		echo "<input id=\"Remark1\" name=\"Remark1\" value=\"".$Remark1."\" type=\"hidden\" />";
		echo "<input id=\"Remark2\" name=\"Remark2\" value=\"".$Remark2."\" type=\"hidden\" />";
		echo "<input id=\"Remark3\" name=\"Remark3\" value=\"".$Remark3."\" type=\"hidden\" />";
		echo "<input id=\"ReturnURL\" name=\"ReturnURL\" value=\"".$ReturnURL."\" type=\"hidden\" />";
		echo "<input id=\"NotifyURL\" name=\"NotifyURL\" value=\"".$NotifyURL."\" type=\"hidden\" />";
		echo "<input id=\"SignInfo\" name=\"SignInfo\" value=\"".$SignInfo."\" type=\"hidden\" />";
		echo "<script language=\"javascript\">window.onload=function(){document.formu.submit();}</script>";
		echo "</form>";
	}


}